package cn.stylefeng.guns.modular.basic.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicAddress;
import cn.stylefeng.guns.modular.basic.mapper.BasicAddressMapper;
import cn.stylefeng.guns.modular.basic.model.params.BasicAddressParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAddressResult;
import  cn.stylefeng.guns.modular.basic.service.BasicAddressService;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.validator.BaseValidatingParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 往来地址表 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2019-07-24
 */
@Service
public class BasicAddressServiceImpl extends ServiceImpl<BasicAddressMapper, BasicAddress> implements BasicAddressService {

    @Override
    public void add(BasicAddressParam param){
        BasicAddress entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BasicAddressParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BasicAddressParam param){
        BasicAddress oldEntity = getOldEntity(param);
        BasicAddress newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BasicAddressResult findBySpec(BasicAddressParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<BasicAddressResult> findListBySpec(BasicAddressParam param){
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(BasicAddressParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<BaseValidatingParam> mapAddress() {
        return this.baseMapper.mapAddress();
    }

    private Serializable getKey(BasicAddressParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BasicAddress getOldEntity(BasicAddressParam param) {
        return this.getById(getKey(param));
    }

    private BasicAddress getEntity(BasicAddressParam param) {
        BasicAddress entity = new BasicAddress();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
